package ngamejava2;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.Area;
import java.io.IOException;
import javax.xml.xpath.XPathExpressionException;
import org.w3c.dom.Node;

public class PlatformObject extends GameObject<GamePlane>
{
	private Area area;
	private Polygon polygon;
	private boolean polygonUpdated;

	public PlatformObject()
	{
		super();
		setCollidable(true);
		polygon = new Polygon();
		polygonUpdated = true;
	}

	@Override
	public void render(Graphics2D g, long currentFrame)
	{
		g.setColor(Color.blue);
		g.fill(getArea());
	}

	public void addPoint(int x, int y)
	{
		polygon.addPoint(x, y);
		getBounds().setRect(polygon.getBounds2D());
		polygonUpdated = true;
	}

	public Area getArea()
	{
		if (polygonUpdated)
		{
			area = new Area(polygon);
			polygonUpdated = false;
		}
		return area;
	}

	@Override
	public void write(GameWriter gw) throws IOException
	{
		super.write(gw);
		gw.write("polygon", polygon);
	}

	@Override
	public void read(GameReader gr, Node objectNode) throws XPathExpressionException
	{
		super.read(gr, objectNode);
		polygon = gr.readPolygon(objectNode, "polygon[@name='polygon']");
		polygonUpdated = true;
	}
}
